// 判断是否存在cookie，不存在就跳转到登录界面
let username = getCookie()

if (!username.login) {
    location.href = './login.html';
}
$('<span></span>').insertBefore($('.tuichu')).html(`欢迎${username.login}管理员登录系统`)
$('li').after($(`<div class="nav"><p>大一</p><div class="zongcebiao"></div><p>大二</p><div class="zongcebiao"></div><p>大三</p><div class="zongcebiao"></div><p>大四</p><div class="zongcebiao"></div>`))
$('li').next('.nav').slideUp()
$('p').next('.zongcebiao').slideUp()
$('li').on('click', function () {
    // 点击左边菜单高亮效果
    $(this).addClass('active').siblings().removeClass('active')
    // 判断下一个兄弟元素
    if ($(this).next().hasClass('nav')) {
        $(this).next('.nav').slideToggle()
    } else {
        $(this).siblings('.nav').slideUp()
    }

    $(this).css({
        boxShadow: '1px 1px 5px 0px rgb(124, 124, 124)'
    }).siblings().css({
        boxShadow: 'none'
    })
})
$('.left p').on('click', function () {
    // 点击左边菜单高亮效果
    $(this).addClass('active').siblings().removeClass('active')
    // 判断下一个兄弟元素
    if ($(this).next().hasClass('zongcebiao')) {
        $(this).next('.zongcebiao').slideToggle()
    } else {
        $(this).siblings('.zongcebiao').slideUp()
        // $('.zongcebiao').children().removeClass('active')
    }
    // if ($(this).next().hasClass('guize')) {
    //     $(this).next('.guize').slideToggle()
    // } else {
    //     $('li').next('.guize').slideUp()
    // }
    $(this).css({
        boxShadow: '1px 1px 5px 0px rgb(124, 124, 124)'
    }).siblings().css({
        boxShadow: 'none'
    })
})

$('.left li').on('click', function () {
    let college = $(this).html()
    // console.log(college);
    let cla, grade;
    $('.left p').on('click', function () {
        if ($(this).parent().hasClass('nav')) {
            grade = $(this).html()
            $.rander(college, grade)
            table(college, grade)
        }
        $('.studentOrder').slideUp()

        function table(college, grade) {
            $('.left p').on('click', function () {
                if ($(this).parent().hasClass('zongcebiao')) {
                    $(this).next('.studentOrder').slideToggle()
                    $(this).addClass('active').siblings().removeClass('active')
                    // console.log($(this).html());

                    cla = $(this).html()
                    // $.rander()

                    // console.log($('.order').html());
                    studentOrder(college, grade, cla)
                }

                function studentOrder(college, grade, cla) {
                    $('.left p').on('click', function () {
                        if ($(this).parent().hasClass('studentOrder')) {
                            let s_Order = $(this).children('.order').html()
                            $(this).next('.student').slideToggle()
                            // console.log(s_Order);
                            $.studentData(college, grade, cla, s_Order)


                        }
                    })
                }
            })
        }

    })

})
$.extend({
    rander: function (college, grade) {
        $.ajax({
            url: "/ap/getCollege.php",
            method: "post",
            data: {
                // sid: username.login,
                grade: grade,
                college: college,
            },
            dataType: "json",
            async: false,
            success: function (res) {
                $.rander1(res)

            },
            error: function (err) {
                // console.log(err);
            },

        });
    },
    rander1: function (data) {
        let Classarr = []

        data.map(function (item) {
            // console.log(item);
            Classarr.push(item.class)
        })
        let s = new Set(Classarr)
        let classArr = [...s]

        let ran = classArr.map(function (ite) {
            // console.log(ite);
            return `<p>${ite}</p><div class="studentOrder">
            <p>第 <span class="order">一</span> 学期</p>
                <div class="student">
                    
                </div>
            <p>第 <span class="order">二</span> 学期</p>
            <div class="student">
                    
                </div>
        </div>`
        }).join('')
        $('.zongcebiao').html(ran)
        $('.stuednt').slideUp()
    },
    studentData: function (college, grade, cla, s_Order) {
        // console.log(college);
        // console.log(cla);
        $.ajax({
            url: "/ap/getTableStudent.php",
            method: "post",
            data: {
                // sid: username.login,
                grade: grade,
                college: college,
                cla: cla,
                s_Order: s_Order
            },
            dataType: "json",
            async: false,
            success: function (r) {
                // $.rander1(res)

                $.studentRanden(r)

            },
            error: function (err) {
                // console.log(err);
            },

        });

    },
    studentRanden: function (Sdata) {
        // console.log(Sdata);
        $('.studentOrder p').on('click', function () {
            // console.log($(this));
            // console.log($(this).addClass());
            $(this).addClass('active').siblings().removeClass('active')
            if ($(this).parent().hasClass('studentOrder')) {
                // console.log($(this));
                let s = Sdata.map(function (iten) {

                    let dataTable = JSON.parse(iten.table)
                    // console.log(dataTable);
                    return `${iten.username?`<p ${dataTable.map(function(T){return `${T.title?`title="${T.title}"`:''}`}).join('')}><span>${iten.username}</span><span class="u_id">${iten.s_id}</span><span class="u_class">${iten.class}</span><span>${iten.college}</span></p>`:''}`
                }).join('')
                // console.log($('.active'));
                $('.studentOrder .active').next('.student').html(s)
                // $('.student p').addClass('active').siblings().removeClass('active')
            }
            $('.student p').on('click', function () {
                $(this).addClass('active').siblings().removeClass('active')
                let u_id = $(this).children('.u_id').html();
                // console.log($(this).children('.u_id').html());
                // console.log($(this).parent().prev());
                let u_order = $(this).parent().prev().children('.order').html()
                // console.log(u_order);
                $.userTable(u_id, u_order)
                $.tijiao()
            })
        })
        // $('.student').slideToggle()
    }
})

$.extend({
    userTable: function (u_id, u_order) {
        // console.log(u_id);
        $.ajax({
            url: "/ap/gettableData.php",
            method: "post",
            data: {
                u_id: u_id,
                u_order: u_order
            },
            dataType: "json",
            async: false,
            success: function (TS) {
                // $.rander1(res)
                // console.log(TS);
                $.userRanden(TS)

            },
            error: function (err) {
                // console.log(err);
            },

        });
    },
    userRanden: function (data) {
        let ran = data.map(function (item) {

            // let res = JSON.parse(item.table);
            return `<h1>玉林师范学院<span class="xueyuan">${item.college}</span><span class="collegeClass">${item.class}</span>学生<span class="writTime">${item.time}</span>学年度第<span class="order">${item.order}</span>学期德智体综合测评量化评分表</h1>`
            // return res.map(function (res) {
            //     // console.log(res);
            //     return `${res.title?`<h1>${res.title}</h1>`:''}`
            // }).join('')

        }).join('')



        let ran1 = data.map(function (item) {

            // let res = JSON.parse(item.table);


            return `<p class="u_name">
                <span>
                    <label for="">姓名：</label>
            
                    <input id="username" type="text" value="${item.username}">
                </span>
                <span>
                    <label for="">学号：</label>
                    <input id="user_id" type="text" value="${item.s_id}">
                </span>
                <span>
                    <label for="">大几：</label>
                    <input id="grade" type="text" value="${item.grade}">
                </span>
            
            </p>
            <button class="add_row">添加一行</button>
            `
        }).join('')
        $('.top').html(ran + ran1)
        let ran2 = data.map(function (item) {

            let res = JSON.parse(item.table);

            return res.map(function (res) {
                // 用三目运算判断是否存在这个数据,存在就渲染
                return `${res.xuhao?` <tr>
                    ${res.xuhao?`<td class="xuhao" contenteditable="true">${res.xuhao}</td>`:'<td class="xuhao" contenteditable="true"></td>'}
                    ${res.jiafen?`<td class="jiafen">${res.jiafen}</td>`:'<td class="jiafen"></td>'}
                    ${res.jianfen?`<td class="jianfen">${res.jianfen}</td>`:'<td class="jianfen"></td>'}
                    ${res.de?`<td class="de">${res.de}</td>`:'<td class="de"></td>'}
                    ${res.zhi?`<td class="zhi">${res.zhi}</td>`:'<td class="zhi"></td>'}
                    ${res.ti?`<td class="ti">${res.ti}</td>`:'<td class="ti"></td>'}  
                    </tr>`:''}
            `
            }).join('')

        }).join('')
        // console.log(ran2);
        $('.one tbody').html(ran2)

        let ran3 = data.map(function (item) {

            let res = JSON.parse(item.table);

            return res.map(function (res) {

                return ` 
                ${res.deyuJBF?`<td>
                <span>德育基本分</span><span contenteditable="true" id="deyuJBF" class="fenshu">${res.deyuJBF}</span></td>`:''}
                ${res.deyuDF?`<td>
                <span>德育得分</span><span contenteditable="true" id="deyuDF" class="fenshu">${res.deyuDF}</span></td>`:''}
                ${res.deyuZF?`<td>
                <span>德育总分</span><span contenteditable="true" id="deyuZF" class="fenshu">${res.deyuZF}</span></td>`:''}
                ${res.zhiyuJBF?`<td>
                <span>智育基本分</span><span contenteditable="true" id="zhiyuJBF" class="fenshu">${res.zhiyuJBF}</span></td>`:''}
                ${res.zhiyuDF?`<td>
                <span>智育得分</span><span contenteditable="true" id="zhiyuDF" class="fenshu">${res.zhiyuDF}</span></td>`:''}
                ${res.zhiyuZF?`<td>
                <span>智育总分</span><span contenteditable="true" id="zhiyuZF" class="fenshu">${res.zhiyuZF}</span></td>`:''}
                ${res.tiyuJBF?`<td>
                <span>体育基本分</span><span contenteditable="true" id="tiyuJBF" class="fenshu">${res.tiyuJBF}</span></td>`:''}
                ${res.tiyuDF?`<td>
                <span>体育得分</span><span contenteditable="true" id="tiyuDF" class="fenshu">${res.tiyuDF}</span></td>`:''}
                ${res.tiyuZF?`<td>
                <span>体育总分</span><span contenteditable="true" id="tiyuZF" class="fenshu">${res.tiyuZF}</span></td>`:''}
            
                        
                    
                    
                       
            `
            }).join('')

        }).join('')
        // console.log(ran3);
        $('.Score').html(ran3)
        let ran4 = data.map(function (item) {

            let res = JSON.parse(item.table);

            return res.map(function (res) {

                return `
                ${res.cepingZF?`<td colspan="2">
                <span>综合测评总分</span><span contenteditable="true" id="cepingZF" class="rink">${res.cepingZF}</span></td>`:``}
            
                        
                        
                ${res.cepingRink?`<td colspan="2">
                <span>综合测评总分在班中名次</span><span contenteditable="true" id="cepingRink" class="rink">${res.cepingRink}</span></td>`:''}
            
                        
                        
                ${res.banzhang?`<td>
                <span>班长签名</span><span contenteditable="true" id="banzhang" class="rink">${res.banzhang}</span></td>`:''}
                
                        
                        
                ${res.banzhuren?`<td colspan="2">
                <span>班主任签名</span><span contenteditable="true" id="banzhuren" class="rink">${res.banzhuren}</span></td>`:''}
            
                        
                        
                ${res.fudaoyuan?`<td colspan="2">
                <span>辅导员签名</span><span contenteditable="true" id="fudaoyuan" class="rink">${res.fudaoyuan}</span></td>`:''}         
            `
            }).join('')

        }).join('')
        // console.log(ran4);
        $('.Rink').html(ran4)
        let ran5 = data.map(function (item) {

            let res = JSON.parse(item.table);

            return `<td colspan="3">此表一式一份由二级学院保存备查。</td>
        <td><b>涂改无效</b> </td>
        <td><b>注意保存</b> </td>
        <td colspan="3">玉林师范学院学生工作部（处）制</td>${res.map(function (res) {
            
            return `
            ${res.time?`<td class="time">${res.time}</td>`:''}
            
                        
            `
        }).join('')}`

        }).join('')
        $('tfoot tr').html(ran5)
        // console.log(ran5);
        let ran6 = data.map(function (item) {

            let res = JSON.parse(item.table);

            return `<span>本学期必修课程名称及成绩分数:</span>${res.map(function (res) {

            return `${res.project?`<p class="project" contenteditable="true">${res.project}</p>`:''}`
        }).join('')}`

        }).join('')
        $('.bottom').html(ran6)
        let ran7 = data.map(function (item) {
            return `<p class="status">${item.status}</p>`

        }).join('')
        $('.stu').html(ran7)
        // 判断数据 的状态，如果是已通过，把表格的三个按钮变为不可点击
        // if ($('.status').html() == '已通过') {
        //     $('.updata').attr('disabled', 'true')
        // } else if ($('.status').html() == '不通过') {
        //     // 如果是不通过就把字体标红
        //     $('.status').css({
        //         color: 'red'
        //     })
        // }
        // 查看已提交的数据表时 ，表格不可编辑
        $('.one td').attr('contenteditable', 'false')
        $('.two .fenshu').attr('contenteditable', 'false')
        $('.two .rink').attr('contenteditable', 'false')

        $('.u_name input ').attr('disabled', 'true')
    },
    tijiao: function () {
        // console.log($('.tijiao'));
        $('.tijiao').on('click', '.adopt', function () {
            // console.log($(this).html());
            let status = $(this).html()
            let user_ID=$('#user_id').val()
            let OD=$('.top .order').html()
            $('.status').html(status)
            $.updataStatus(status,user_ID,OD)

        })
        $('.tijiao').on('click', '.Fail', function () {
            let status = $(this).html()
            let user_ID=$('#user_id').val()
            let OD=$('.top .order').html()
            $('.status').html(status)
            $.updataStatus(status,user_ID,OD)

        })
    },
    updataStatus: function (status,user_ID,OD) {
        $.post(
            "/ap/updataStatus.php", {
                
                sid: user_ID,
                status: status,
                order:OD,
                
            },
            function (res) {},
            "json"
        );
    }
})






$('.tuichu').on('click', function () {
    Cookies.remove('login')
    let login = getCookie()

    if (!login.login) {
        location.href = './login.html';
    }
})